﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GreateTool
{
    public partial class TextMergeForm : Form
    {
        public TextMergeForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int count;
            var refrence = "reference: ";
            
            Dictionary<string, int> dic = new Dictionary<string, int>();

            foreach (var line in richTextBox1.Lines)
            {
                var index1 = line.IndexOf(refrence);
                if (index1 < 0) continue;
                index1 += refrence.Length;
                var index2 = line.IndexOf(' ', index1);
                var str = line.Substring(index1, index2 - index1);

                if (dic.TryGetValue(str, out count)) dic[str] = count + 1;
                else dic.Add(str, 1);
            }

            richTextBox1.Clear();

            StringBuilder sb = new StringBuilder();

            foreach (var kv in dic)
            {
                sb.Append(kv.Key).Append("\t").Append(kv.Value).Append(Environment.NewLine);    
            }

            richTextBox1.Text = sb.ToString();
        }
    }
}
